package com.ss.android.article.base.feature.pgc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.ttnet.a.AppConfig;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.c.ImageManager;
import com.ss.android.article.base.feature.e.i;
import com.ss.android.article.base.feature.e.m;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;
import com.ss.android.article.base.ui.BaseActionDialog;
import com.ss.android.article.base.ui.BaseActionDialog.CtrlFlag;
import com.ss.android.article.base.ui.BaseActionDialog.DisplayMode;
import com.ss.android.article.browser.R;
import com.ss.android.article.common.share.factory.ShareHelperFactory;
import com.ss.android.article.common.share.helper.CommonWXShareHelper;
import com.ss.android.article.share.e.b;
import com.ss.android.article.share.entity.ShareAction;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.ImmersedStatusBarHelper;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.newmedia.activity.browser.BrowserActivity;
import com.ss.android.newmedia.util.AppUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.EnumSet;

public class PgcActivity extends BrowserActivity implements b {
    static final class a extends AsyncTask {
        private final PgcActivity a;

        a(PgcActivity arg1, byte arg2) {
            this(arg1);
        }

        private a(PgcActivity arg1) {
            this.a = arg1;

        }

        private static EntryItem a(Long[] arg5) {
            EntryItem entryItem = null;
            if (arg5.length != 0) {
                try {
                    entryItem = android.support.design.a.d(arg5[0].longValue());
                } catch (Throwable v1) {
                    Logger.w("PgcActivity", "get pgc profile exception: " + v1);
                }
            }

            return entryItem;
        }

        protected final Object doInBackground(Object[] arg2) {
            return a(((Long[]) arg2));
        }

        protected final void onPostExecute(Object arg4) {
            if (arg4 != null) {
                this.a.a = ((EntryItem) arg4);
                if (PgcActivity.a(this.a) != null) {
                    PgcActivity.b(this.a).setText(this.a.a.mName);
                }

                if (android.support.design.a.isTextEmpty(((EntryItem) arg4).mIconUrl)) {
                    return;
                }

                AppUtil.a(this.a, new ImageInfo(((EntryItem) arg4).mIconUrl, null), new ImageManager(this.a));
            }
        }
    }

    EntryItem a;
    private Spipe g;
    private AppData h;
    private int i;
    private String j;
    private int k;
    private String l;
    private JSONObject m;
    private boolean n;
    private long o;
    private long p;
    private String q;
    private m r;

    public PgcActivity() {

        this.k = -1;
        this.n = false;
        this.o = 0;
        this.p = 0;
        this.q = "pgc_profile";
    }

    public final void a(long arg12) {
        long v8 = 0;
        int v7 = -2;
        if ((this.isActive()) && this.a != null && arg12 > v8 && this.o == arg12) {
            EnumSet v6 = EnumSet.of(CtrlFlag.disableAlipay);
            if (this.r.a()) {
                v6.add(CtrlFlag.hasWeixin);
            }

            BaseActionDialog v0 = new BaseActionDialog(this, this, 205, this.q, DisplayMode.PGC_SHARE, v6);
            v0.getWindow().setLayout(v7, v7);
            v0.show();
            v0.e = this.a.mId;
            MobClickCombiner.onEvent(this, this.q, "share_button", this.a.mId, v8);
        }
    }

    public static void startActivity(Context context, long mediaId, String enterFrom) {
        PgcActivity.startActivity(context, mediaId, 0, enterFrom, -1);
    }

    public static void startActivity(Context context, long mediaId, long itemId, String enterFrom) {
        PgcActivity.startActivity(context, mediaId, itemId, enterFrom, -1);
    }

    public static void startActivity(Context context, long mediaId, long itemId, String enterFrom, int pageType) {
        Intent intent = new Intent(context, PgcActivity.class);
        intent.putExtra("mediaid", mediaId);
        intent.putExtra("itemid", itemId);
        intent.putExtra("use_swipe", true);
        intent.putExtra("enter_from", enterFrom);
        intent.putExtra("page_type", pageType);
        context.startActivity(intent);
    }

    static TextView a(PgcActivity arg1) {
        return arg1.mTitleView;
    }

    private String a(Intent arg4) {
        String v0;
        if (arg4 == null) {
            v0 = null;
            return v0;
        }

        v0 = arg4.getStringExtra("enter_from");
        if (android.support.design.a.isTextEmpty(this.l)) {
            String v1 = arg4.getStringExtra("gd_ext_json");
            if (android.support.design.a.isTextEmpty(v1)) {
                return v0;
            }

            try {
                v0 = new JSONObject(v1).getString("enter_from");
            } catch (Exception v1_1) {
                v1_1.printStackTrace();
            }
        }

        return v0;
    }

    private void a(String arg4, ShareAction arg5) {
        if (this.a != null) {
            ShareHelperFactory.getInstance();
            if (ShareHelperFactory.getActionHelper(((Context) this), arg5, AppData.getInstance()).doAction(this.a, new Object[0])) {
                this.d(arg4);
            }
        }
    }

    private void a(String arg7, String arg8) {
        try {
            JSONObject v0_1 = new JSONObject();
            v0_1.put("enter_from", arg8);
            if (this.p != 0) {
                v0_1.put("item_id", this.p);
            }

            this.a(arg7, "enter", v0_1);
        } catch (Exception v0) {
        }
    }

    private void a(String arg10, String arg11, JSONObject arg12) {
        MobClickCombiner.onEvent(this, arg10, arg11, this.o, 0, arg12);
    }

    public final boolean a(com.ss.android.article.share.entity.a arg10, View arg11, com.ss.android.article.share.c.a arg12) {
        boolean v0;
        int v0_2;
        String[] v2 = null;
        boolean v6 = false;
        if (arg10 != null) {
            switch (arg10.e) {
                case 1: {
                    this.b(1);
                    v0 = true;
                    break;
                }
                case 2: {
                    this.b(0);
                    v0 = true;
                    break;
                }
                case 3: {
                    this.a("share_qq", ShareAction.qq);
                    v0 = true;
                    break;
                }
                case 4: {
                    this.a("share_qzone", ShareAction.qzone);
                    v0 = true;
                    break;
                }
                case 10: {
                    this.a("share_system", ShareAction.link);
                    v0 = true;
                    break;
                }
                case 20: {
                    if (this.a != null && !android.support.design.a.isTextEmpty(this.a.mName)) {
                        String v5 = this.a.mName;
                        if (!android.support.design.a.isTextEmpty(v5)) {
                            String v1 = Build.VERSION.SDK_INT < 19 ? "content://com.android.launcher.settings/favorites?notify=true" : "content://com.android.launcher3.settings/favorites?notify=true";
                            Cursor v0_1 = this.getContentResolver().query(Uri.parse(v1), v2, "title=?", new String[]{v5}, ((String) null));
                            if (v0_1 == null) {
                                v0_2 = 0;
                            } else if (!v0_1.moveToFirst()) {
                                v0_2 = 0;
                            } else {
                                v0_1.close();
                                v0_2 = 1;
                            }
                        } else {
                            v0_2 = 0;
                        }

                        if (v0_2 == 0) {
                            Intent v0_3 = new Intent();
                            v0_3.setClass(((Context) this), PgcActivity.class);
                            v0_3.setAction("android.intent.action.MAIN");
                            v0_3.addCategory("android.intent.category.LAUNCHER");
                            v0_3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            v0_3.putExtra("mediaid", this.a.mId);
                            v0_3.putExtra("use_swipe", true);
                            v0_3.putExtra("enter_from", "desktop");
                            v0_3.putExtra("page_type", -1);
                            Intent v1_1 = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
                            v1_1.putExtra("duplicate", false);
                            v1_1.putExtra("android.intent.extra.shortcut.NAME", this.a.mName);
                            String v3 = "android.intent.extra.shortcut.ICON";
                            if (this.a != null && !android.support.design.a.isTextEmpty(this.a.mIconUrl)) {
                                ImageInfo v4 = new ImageInfo(this.a.mIconUrl, ((String) null));
                                ImageManager v2_1 = new ImageManager(((Context) this));
                                int v5_1 = ((int) UIUtils.dp2px(((Context) this), 48f));
                                Bitmap v2_2 = v2_1.a(v4.mKey, v5_1, v5_1);
                            }

                            v1_1.putExtra(v3, ((Parcelable) null));
                            v1_1.putExtra("android.intent.extra.shortcut.INTENT", v0_3);
                            this.sendBroadcast(v1_1);
                        }

                        MobClickCombiner.onEvent(this, "pgc_profile", "add_entrance", this.a.mId, 0);
                        g.a(this, R.drawable.doneicon_popup_textpage, R.string.add_to_desktop_success);
                        v0 = true;
                        return v0;
                    }
                    v0 = true;
                    break;
                }
                default: {
                    v0 = false;
                    break;
                }
            }

            label_8:
            v6 = v0;
        }

        return v6;
    }

    static TextView b(PgcActivity arg1) {
        return arg1.mTitleView;
    }

    private void b(int arg4) {
        CommonWXShareHelper v0 = CommonWXShareHelper.getInstance(((Context) this), AppData.getInstance(), arg4);
        v0.b();
        v0.a(this.a, new Object[0]);
        this.d(CommonWXShareHelper.b(arg4));
    }

    public final boolean b() {
        return true;
    }

    private void d(String arg9) {
        long v6 = 0;
        if (!android.support.design.a.isTextEmpty(arg9)) {
            long v4 = this.a != null ? this.a.mId : v6;
            MobClickCombiner.onEvent(this, "pgc_profile", arg9, v4, v6);
        }
    }

    public ImmersedStatusBarHelper.ImmersedStatusBarConfig getImmersedStatusBarConfig() {
        return new ImmersedStatusBarHelper.ImmersedStatusBarConfig().setStatusBarColor(com.ss.android.article.browser.R.color.status_bar_color_detail);
    }

    public void init() {
        String v2;
        String v0;
        int v8 = -1;
        long v6 = 0;
        new i(((Activity) this));
        this.r = com.ss.android.article.base.feature.e.m.a(((Context) this));
        this.g = Spipe.inst();
        this.h = AppData.getInstance();
        Intent v1 = this.getIntent();
        if (v1 != null) {
            this.o = v1.getLongExtra("mediaid", v6);
            this.p = v1.getLongExtra("itemid", v6);
            this.i = v1.getIntExtra("list_type", v8);
            this.j = v1.getStringExtra("from_category");
            v0 = v1.getStringExtra("gd_ext_json");
            v2 = v1.getStringExtra("growth_from");
            if (!android.support.design.a.isTextEmpty(v0)) {
                try {
                    this.m = new JSONObject(v0);
                } catch (JSONException v0_1) {
                    Logger.e("PgcActivity", "exception in initView " + v0_1.toString());
                }
            }

            if (!android.support.design.a.isTextEmpty(v2)) {
                this.a("pgc_profile", v2, this.m);
            }

            this.l = this.a(v1);
            this.k = v1.getIntExtra("page_type", v8);
        }

        if (this.o <= v6) {
            super.init();
            if (!this.isFinishing()) {
                this.finish();
            }
        } else {
            if (v1 != null) {
                Uri.Builder v0_2 = Uri.parse(AppConfig.getInstance(((Context) this)).c(String.format(ArticleConstants.m, Long.valueOf(this.o)))).buildUpon();
                v0_2.scheme("https");
                if (this.k >= 0) {
                    v0_2.appendQueryParameter("page_type", String.valueOf(this.k));
                }

                v0_2.appendQueryParameter("client_dialog_show", "true");
                Uri.Builder v2_1 = Uri.parse(AppLog.a(v0_2.toString(), false)).buildUpon();
                StringBuilder v3 = new StringBuilder("tt_daymode=");
                v0 = AppData.isNightMode() ? "0" : "1";
                v1.setData(v2_1.encodedFragment(v3.append(v0).toString()).build());
            }

            super.init();
            boolean v0_3 = this.g.pgcMediaid == this.o ? true : false;
            this.n = v0_3;
            if (this.n) {
                this.q = "my_pgc_profile";
            }

            if (this.l != null) {
                this.a(this.q, this.l);
            } else {
                v2 = this.q;
                long v4 = this.o;
                JSONObject v8_1 = this.m;
                v0 = "";
                if (android.support.design.a.isTextEmpty(v0)) {
                    if (this.i == 1) {
                        if ("__all__".equals(this.j)) {
                            v0 = "click_headline";
                        } else if (!android.support.design.a.isTextEmpty(this.j)) {
                            v0 = "click_" + this.j;
                        }
                    } else if (this.i == 3) {
                        v0 = "click_search";
                    } else if (this.i == 4) {
                        v0 = "click_pgc_list";
                    } else if (this.i == 2) {
                        v0 = "click_favorite";
                    } else if (this.i == 8) {
                        v0 = "click_read_history";
                    } else if (this.i == 9) {
                        v0 = "click_push_history";
                    } else if (this.i == 10) {
                        v0 = "click_refresh_history";
                    }
                }

                String v3_1 = android.support.design.a.isTextEmpty(v0) ? "enter" : v0;
                if (android.support.design.a.isTextEmpty(v2)) {
                } else {
                    MobClickCombiner.onEvent(this, v2, v3_1, v4, v6, v8_1);
                }
            }

            if (!NetworkUtils.isNetworkAvailable(((Context) this))) {
                return;
            }

            new a(this, (byte) 0).execute(new Long[]{Long.valueOf(this.o)});
        }
    }

    public void onBackBtnClick() {
        if (this.isViewValid()) {
            if ("desktop".equals(this.l)) {
                Intent v0 = null;
                if (this.isTaskRoot()) {
                    v0 = ToolUtils.getLaunchIntentForPackage(((Context) this), this.getPackageName());
                }

                if (v0 == null) {
                    super.onBackBtnClick();
                    return;
                }

                this.finish();
                if (this.h != null) {
                    System.currentTimeMillis();
                    AppData.ah();
                }

                v0.putExtra("quick_launch", true);
                this.startActivity(v0);
            }
            super.onBackBtnClick();
            return;
        }
    }

    public void onNewIntent(Intent arg7) {
        long v4 = 0;
        super.onNewIntent(arg7);
        if (arg7 != null) {
            long v0 = arg7.getLongExtra("mediaid", v4);
            if (v0 == this.o) {
                this.p = arg7.getLongExtra("itemid", v4);
                this.l = this.a(arg7);
                if (!android.support.design.a.isTextEmpty(this.l)) {
                    this.a(this.q, this.l);
                }
            } else {
                arg7.putExtra("mediaid", v0);
                this.setIntent(arg7);
                this.init();
            }
        }
    }
}

